/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.project;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.config.Global;
import com.idenger.common.utils.StringUtils;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.EduCourse;
import com.idenger.modules.edu.entity.project.EduProject;
import com.idenger.modules.edu.entity.project.EduProjectCourse;
import com.idenger.modules.edu.service.course.EduCourseService;
import com.idenger.modules.edu.service.project.EduProjectCourseService;
import com.idenger.modules.edu.service.project.EduProjectService;
import com.idenger.modules.edu.web.comm.Constant;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.persistence.Page;

/**
 * 项目Controller
 * @author chenlin
 * @version 2017-12-04
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/project/eduProject")
public class EduProjectController extends BaseController {

	@Autowired
	private EduProjectService eduProjectService;
	@Autowired
	private EduProjectCourseService eduProjectCourseService;
	@Autowired
	private EduCourseService eduCourseService;

	@ModelAttribute
	public EduProject get(@RequestParam(required=false) String id) {
		EduProject entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduProjectService.get(id);
		}
		if (entity == null){
			entity = new EduProject();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:project:eduProject:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduProject eduProject, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduProject> page = eduProjectService.findPage(new Page<EduProject>(request, response), eduProject); 
		model.addAttribute("page", page);
		return "modules/edu/project/eduProjectList";
	}

	@RequiresPermissions("edu:project:eduProject:view")
	@RequestMapping(value = "form")
	public String form(EduProject eduProject, Model model) {

		//查询这个项目下的课程根据projectId 查询projectCourse
		String pid ="";
		if(eduProject.getId()!=null){
			EduProjectCourse eduProjectCourse = new EduProjectCourse();
			eduProjectCourse.setProjectId(eduProject.getId());
			List<EduProjectCourse> eduProjectCourseList = eduProjectCourseService.findList(eduProjectCourse);
			eduProject.setEduProjectCourseList(eduProjectCourseList);
			pid = eduProject.getId();
		}

		//查询所	有的课程。
		
		List<EduCourse> eduCoursesList = eduCourseService.getEmptyCourse(pid);
	
		model.addAttribute("eduCoursesList",eduCoursesList);
		model.addAttribute("eduProject", eduProject);
		return "modules/edu/project/eduProjectForm";
	}

	@RequiresPermissions("edu:project:eduProject:edit")
	@RequestMapping(value = "save")
	public String save(EduProject eduProject, Model model, RedirectAttributes redirectAttributes) {
		if(StringUtils.isBlank(eduProject.getId())){
			eduProject.setStatus(Constant.PROJECT_STATUS_NO);//项目未发布
		}
		//优惠价原价
		BigDecimal pp = new BigDecimal("0.00");
		if(StringUtils.isNotBlank(eduProject.getPreferentialPrice())){
			pp = new BigDecimal(eduProject.getPreferentialPrice());
		}
		pp.setScale(2,BigDecimal.ROUND_HALF_UP);//四舍五入
		eduProject.setPreferentialPrice(pp.toString());

		BigDecimal p = new BigDecimal("0.00");
		if(StringUtils.isNotBlank(eduProject.getProjectPrice())){
			p = new BigDecimal(eduProject.getProjectPrice());
		}
		p.setScale(2,BigDecimal.ROUND_HALF_UP);
		eduProject.setProjectPrice(p.toString());
		//学时学分
		if(StringUtils.isBlank(eduProject.getScore())){
			eduProject.setScore("0");
		}
		if(StringUtils.isBlank(eduProject.getStartPeriod())){
			eduProject.setStartPeriod("0");
		}
		List<EduProjectCourse> eduProjectCourseList = eduProject.getEduProjectCourseList();
		eduProjectService.save(eduProject);
		//根据项目ID查询
		EduProjectCourse eduPCourse = new EduProjectCourse();
		eduPCourse.setProjectId(eduProject.getId());
		List<EduProjectCourse> eduPCList = eduProjectCourseService.findList(eduPCourse);
		for (EduProjectCourse eduProjectCourse2 : eduPCList) {
			eduProjectCourseService.delete(eduProjectCourse2);
		}
		//保存项目下的课程
		if(eduProjectCourseList!=null && eduProjectCourseList.size()>0){
			for (EduProjectCourse eduProjectCourse : eduProjectCourseList) {
				eduProjectCourse.setProjectId(eduProject.getId());
				eduProjectCourseService.save(eduProjectCourse);
			}
		}
		addMessage(redirectAttributes, "保存项目成功");
		return "redirect:"+ Global.getAdminPath()+"/edu/project/eduProject/?repage";
	}

	@RequiresPermissions("edu:project:eduProject:edit")
	@RequestMapping(value = "updateProjectStatus")
	public String updateProjectStatus(EduProject eduProject , HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String projectStatus = request.getParameter("projectStatus");
		if(Constant.PROJECT_STATUS_NO.equals(projectStatus)){
			//取消发布
			eduProject.setStatus(Constant.PROJECT_STATUS_NO);
		}else{
			//发布
			eduProject.setStatus(Constant.PROJECT_STATUS_YES);
		}
		eduProjectService.save(eduProject);
		return "redirect:"+Global.getAdminPath()+"/edu/project/eduProject/list?repage";
	}

	@RequiresPermissions("edu:project:eduProject:edit")
	@RequestMapping(value = "delete")
	public String delete(EduProject eduProject, RedirectAttributes redirectAttributes) {
		eduProjectService.delete(eduProject);
		addMessage(redirectAttributes, "删除项目成功");
		return "redirect:"+Global.getAdminPath()+"/edu/project/eduProject/?repage";
	}
	@RequestMapping(value = "findList")
	public String findList(EduProject eduProject, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response){
		BaseResponseMsg baseResponseMsg;
		List<EduProject> list = eduProjectService.findList(eduProject);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(list);
		return renderString(response, baseResponseMsg);
	}
}